<?php


class AdminController extends \Phalcon\Mvc\Controller
{

    public $admin_info;
    public function validate()
    {
        $admin_id = $this->session->get('admin_id');
        if(!$admin_id) $this->response->redirect(BASE_URI.'admin/login');
        $admin_info = AdminAccount::findFirstById($admin_id);
        $this->admin_info = $admin_info;
        $this->view->setVar('admin_info',$admin_info);

    }
    public function logoutAction()
    {
        $this->session->remove('admin_id');
        $this->response->redirect(BASE_URI.'admin/login');
        $this->view->disable();
    }
    public function indexAction()
    {

        $this->validate();
		$this->view->setMainView('admin');
		$this->view->setVar('page_name', 'admin');
		$this->view->setVar('content', 'admin/index');
		$this->view->setVar('title', 'Admin');
        
    }
    public function profileAction()
    {
        $this->validate();
		$this->view->setMainView('admin');
		$this->view->setVar('page_name', 'adminProfile');
		$this->view->setVar('content', 'admin/index');
		$this->view->setVar('title', 'Profile');
    }
    public function loginAction()
    {
        $this->view->setMainView('admin');
        $this->view->setVar('page_name', 'adminLog');
        $this->view->setVar('content', 'admin/login');
        $this->view->setVar('title', 'Profile');
    }
    public function dashboardAction()
    {
        $this->validate();
		$this->view->setMainView('admin');
		$this->view->setVar('page_name', 'dashboardAdmin');
		$this->view->setVar('content', 'admin/index');
		$this->view->setVar('title', 'Company');
    }
    public function reportedcompanyAction()
    {
        $this->validate();
		$this->view->setMainView('admin');
		$this->view->setVar('page_name', 'reportedCompany');
		$this->view->setVar('content', 'admin/index');
		$this->view->setVar('title', 'Company');
    }
    public function postAction()
    {
        $this->validate(); 
		$this->view->setMainView('admin');
		$this->view->setVar('page_name', 'postAdmin');
		$this->view->setVar('content', 'admin/index');
		$this->view->setVar('title', 'Company');
    }
    public function companyAction()
    {
        $this->validate();
		$this->view->setMainView('admin');
		$this->view->setVar('page_name', 'adminCompany');
		$this->view->setVar('content', 'admin/index');
		$this->view->setVar('title', 'Company');

		$user_id = $this->session->get('user_id');
		
		$active = ClientInfo::listCompany(1);
		$this->view->setVar("active",$active);
    }
    public function pendingcompanyAction()
    {
        $this->validate();
		if($this->admin_info->approve != "on"){
            $this->response->redirect(BASE_URI.'admin');
        }
        $this->view->setMainView('admin');
		$this->view->setVar('page_name', 'pendingCompany');
		$this->view->setVar('content', 'admin/index');
		$this->view->setVar('title', 'Company');

		$pending = ClientInfo::listCompany(0);
		$this->view->setVar("pending",$pending);
    }
    public function suggestedEditsAction()
    {
        $this->validate();
        $this->view->setMainView('admin');
        $this->view->setVar('page_name', 'suggestedEdits');
        $this->view->setVar('content', 'admin/index');
        $this->view->setVar('title', 'Company');

        $suggested = ClientInfoSuggests::listSuggestEdit();
        $this->view->setVar("suggested",$suggested);

    }
    public function suspendedcompanyAction()
    {
        $this->validate();
		$this->view->setMainView('admin');
		$this->view->setVar('page_name', 'suspendedCompany');
		$this->view->setVar('content', 'admin/index');
		$this->view->setVar('title', 'Company');

		$suspended = ClientInfo::listCompany(2);
		$this->view->setVar("suspended",$suspended);
    }
    public function groupAction()
    {
        $this->validate();
		$this->view->setMainView('admin');
		$this->view->setVar('page_name', 'groupAdmin');
		$this->view->setVar('content', 'admin/index');
		$this->view->setVar('title', 'Company');
    }
    public function usersAction()
    {
        $this->validate();
        
        if($this->admin_info->modify != "on"){
            $this->response->redirect(BASE_URI.'admin');
        }

		$this->view->setMainView('admin');
		$this->view->setVar('page_name', 'usersAdmin');
		$this->view->setVar('content', 'admin/index');
		$this->view->setVar('title', 'Company');
    }
    public function forumAction()
    {
        $this->validate();
		$this->view->setMainView('admin');
		$this->view->setVar('page_name', 'forumAdmin');
		$this->view->setVar('content', 'admin/index');
		$this->view->setVar('title', 'Company');
    }
     public function changestatusAction()
    {
    	 $company_id = $this->request->getPost('company_id');
    	 $status = $this->request->getPost('status');
         $admin_id = $this->session->get('admin_id');
    	 $client = ClientInfo::findFirstById($company_id);
    	 $client->status = $status;
    	 $client->admin_id = $admin_id;
         $result = array();
         $result['status'] = $client->save();
         echo json_encode($result);
         $this->view->disable();
    }
    public function changeaccountstatusAction()
    {
         $admin_id = $this->request->getPost('admin_id');
         $status = $this->request->getPost('status');
         $account = AdminAccount::findFirstById($admin_id);
         $account->status = $status;
         $result = array();
         $result['status'] = $account->save();
         echo json_encode($result);
         $this->view->disable();
    }
    public function deleteAction()
    {
    	 $company_id = $this->request->getPost('company_id');
    	 $status = $this->request->getPost('status');
        
    	 $client = ClientInfo::findFirstById($company_id);
         $result = array();
         $result['status'] = $client->delete();
         echo json_encode($result);
         $this->view->disable();
     }
}

